package com.zysstudy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author zhaoyoushui
 * @date 2024/4/29 19:59
 * @Description
 * @Copyright 南京优通信息科技股份有限公司
 */
public class StandardJDBCSapmle {

    public static void main(String[] args) {
        // 1.加载并注册jdbc驱动 本质是通知JDBC注册这个驱动类
        Connection conn = null;
        try {
            String connUrlStr = "jdbc:mysql://192.168.100.230:3306/mybatis?useSSl=true&sueUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
            String userName = "root";
            String password = "utooo520";

            // 1.加载并初始化jdbc 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.创建数据库连接

            conn = DriverManager.getConnection(connUrlStr, userName, password);

            //3.创建Statement对象
            Statement stmt = conn.createStatement();


            ResultSet rs = stmt.executeQuery("select * from user");

            //4.遍历查询结果
            while (rs.next()) {
                Integer id = rs.getInt(1);
                String name = rs.getString("name");
                String pwd = rs.getString("pwd");
                System.out.println(id + "--" + name + "--" + pwd);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null && conn.isClosed() == false) {
                    conn.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }


    }
}
